/*
	FileName : app_RTC.c
	Description : 
		RTC hardware calendar.
	Created on 2013.10.14 by ZZP.
	
	Revision History:
	
*/

#include "ucos_ii.h"
#include "bsp.h"
#include "app_rtc.h"
#include  "app.h"
#include "MiscFnx.h"
#include "JT_C_TAX.h"
//////////////////////////////////////////////
///		Local functions and variables
//////////////////////////////////////////////

#define ENABLE_DEBUG_ALARM_TIME			0
#if ENABLE_DEBUG_ALARM_TIME
__IO uint16_t _AlarmErrF = 0;
#endif

#define TIME_CALIB_LIMIT	30




//////////////////////////////////////////////
///		Global function realizations
//////////////////////////////////////////////
void  AppTaskRTC (void *p_arg)
{
	uint8_t err;
	static uint16_t calif = 0;/// _Tm5s = 0;
	uint32_t tm1, tm2;
#if OS_CRITICAL_METHOD == 3                     
	OS_CPU_SR  cpu_sr = 0;
#endif
	(void)p_arg;
	
	while (DEF_TRUE){

		OSSemPend(Sem_RtcAlarm, 3000, &err);
		if (err){
			
			if (!AppInfo.RdyFlag)
				AppInfo.RdyFlag = 1; 
			AppInfo.RtcErr = 1;
			AppInfo.ResetF = 78;
			while (1){}
		}
		else
			AppInfo.RtcErr = 0;
		
		if (AppInfo.RdyFlag){
			
			///_Tm5s++;
			if (AppInfo.TaxDataValid39type){
				
				///tm1 = *(uint32_t *)&(_tmpRxBuf3[3]);
				tm1 = *(uint32_t *)&(JTC_TAX_DATA[TAX38_FRAME_LEN + 3]);
				if (CheckTimeValid(TIME_SRC_TAX, (void *)&tm1))
					AppInfo.TaxTime = tm1;
			}
			
			if (AppInfo.UserTimeValid){
				
				AppInfo.UserTimeValid = 0;
				tm1 = AppInfo.UserTime;
				tm2 = 0;///AppInfo.NowTime;
				CalibrateTime(TIME_SRC_USER, tm1, tm2);
			}
			else if (AppInfo.TaxTimeValidtype){
		
				///if (_Tm5s >= TIME_CALIB_LIMIT)
				if (!calif){
					
					///_Tm5s = 0;
					calif = 1;
					
					OS_ENTER_CRITICAL();
		
						tm1 = AppInfo.TaxTime;
						tm2 = AppInfo.NowTime;
		
					OS_EXIT_CRITICAL();
				
					CalibrateTime(TIME_SRC_TAX, tm1, tm2);
				}
			}
		}
		else
			OSTimeDly(10);
		
		AppInfo.TaskRtcCnt++;
	}
}

uint32_t App_RTC_Callback(unsigned char Arg, void *ptr)
{
	uint32_t tmp;
#if ENABLE_DEBUG_ALARM_TIME	
	uint8_t i, j;
	static uint32_t _LastAlarm = 0x000000ff;
#endif
	
	if (AppInfo.RdyFlag){
		
		tmp = RTC_TimeStructure.RTC_Seconds + ((uint16_t)RTC_TimeStructure.RTC_Minutes << 6) + 
			((uint32_t)RTC_TimeStructure.RTC_Hours << 12);
		tmp |= (((uint32_t)RTC_DateStructure.RTC_Date << 17) + ((uint32_t)RTC_DateStructure.RTC_Month << 22) + 
			((uint32_t)RTC_DateStructure.RTC_Year << 26));
		
		AppInfo.NowTime = tmp;
		AppInfo.OneSecAlarmFlag = 1;
	}
	
	AppInfo.RtcErr = 0;
	Tm10ms = 0;
	
	///if (AppInfo.RdyFlag)
		OSSemPost(Sem_RtcAlarm);

#if ENABLE_DEBUG_ALARM_TIME	
	///Test only
	i = tmp & 0x3f;
	j = _LastAlarm & 0x3f;
	if ((_LastAlarm != 0x000000ff) && (i != j)){
					
		if (((i == 0) && (j != 59)) || ((i != 0) && (j != (i - 1))))
			_AlarmErrF++;
	}
	_LastAlarm = tmp;
#endif
		
	///Test Only:
	////GPIOC->ODR ^= GPIO_Pin_13;
	
	return 0;
}
























